/**
 * 
 */
package com.thinkgsp.jxc.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.thinkgsp.util.XlsFieldAnnotation;
import com.thinkgsp.util.XlsValueType;

/**
 * 客户往来表
 * @author evan
 *
 */
public class RpContact extends BaseModel implements Serializable {

	private static final long serialVersionUID = -6503879850704070090L;

	/**
	 * 往来类型：应收单
	 */
	public final static int CONTACT_TYPE_AREC_BILL = 1;
	/**
	 * 往来类型：其他应收单
	 */
	public final static int CONTACT_TYPE_OTHER_AREC_BILL = 2;
	/**
	 * 往来类型：收款单
	 */
	public final static int CONTACT_TYPE_REC_BILL = 3;
	/**
	 * 往来类型：预收单
	 */
	public final static int CONTACT_TYPE_PREC_BILL = 4;
	/**
	 * 往来类型：退款单(收款)
	 */
	public final static int CONTACT_TYPE_REFUND_REC_BILL = 5;
	/**
	 * 往来类型：应收款转销单
	 */
	public final static int CONTACT_TYPE_AREC_TRANSFER = 6;
	/**
	 * 往来类型：应收款冲应付款
	 */
	public final static int CONTACT_TYPE_AREC_OFFSET_APAY = 7;
	/**
	 * 往来类型：应付单
	 */
	public final static int CONTACT_TYPE_APAY_BILL = -1;
	/**
	 * 往来类型：其他应付单
	 */
	public final static int CONTACT_TYPE_OTHER_APAY_BILL = -2;
	/**
	 * 往来类型：付款单
	 */
	public final static int CONTACT_TYPE_PAY_BILL = -3;
	/**
	 * 往来类型：预付单
	 */
	public final static int CONTACT_TYPE_PPAY_BILL = -4;
	/**
	 * 往来类型：退款单(付款)
	 */
	public final static int CONTACT_TYPE_REFUND_PAY_BILL = -5;
	/**
	 * 往来类型：应付款转销
	 */
	public final static int CONTACT_TYPE_APAY_TRANSFER = -6;
	/**
	 * 往来类型：应付款冲应收款s
	 */
	public final static int CONTACT_TYPE_APAY_OFFSET_AREC = -7;
	
	private Long id;
	private Long arpBillId;
	private Long rpBillId;
	private Integer direct;
	private Integer typeId;
	@XlsFieldAnnotation(ChsName="单据编号",MethodName="getBillCode")
	private String billCode;
	@XlsFieldAnnotation(ChsName="单据日期",MethodName="getBillDate")
	private Date billDate;
	@XlsFieldAnnotation(ChsName="单据类型",MethodName="getTypeName")
	String billType="";
	private Integer custSuppId;
	private Integer deptId;
	private Integer empId;
	private BigDecimal amount;
	private BigDecimal remAmount;
	private Integer checkStatus;
	@XlsFieldAnnotation(ChsName="年份",MethodName="getFyear")
	private Integer fyear;
	@XlsFieldAnnotation(ChsName="期间",MethodName="getPeriod")
	private Integer period;
	@XlsFieldAnnotation(ChsName="摘要",MethodName="getRemark")
	private String remark;
	
	
	
	
	@XlsFieldAnnotation(ChsName="客户",MethodName="getCustomer",RefType=XlsValueType.RefClass)
	private Customer customer;
	private Dept dept;
	private Emp emp;
	
	private boolean includeCheckTag;  //应收或应付金额是否包含核销标志
	
	/**
	 * 标示应收应付金额是否包含核销标记
	 * getARecAmount,getAPayAmount,getRecAmount,getPayAmount
	 * @return the includeCheckTag
	 */
	public boolean isIncludeCheckTag() {
		return includeCheckTag;
	}

	/**
	 * 本方法只有在查询客户往来对账数据时需要设置是否包含核销
	 * @param includeCheckTag the includeCheckTag to set
	 */
	public void setIncludeCheckTag(boolean includeCheckTag) {
		this.includeCheckTag = includeCheckTag;
	}

	/**
	 * 表ID
	 * @return
	 */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 应收应付单据ID
	 * @return
	 */
	public Long getArpBillId() {
		return arpBillId;
	}

	public void setArpBillId(Long arpBillId) {
		this.arpBillId = arpBillId;
	}

	/**
	 * 收款付款单据ID
	 * @return
	 */
	public Long getRpBillId() {
		return rpBillId;
	}

	public void setRpBillId(Long rpBillId) {
		this.rpBillId = rpBillId;
	}

	/**
	 * 应收应付方向
	 * @return
	 */
	public Integer getDirect() {
		return direct;
	}

	public void setDirect(Integer direct) {
		this.direct = direct;
	}

	/**
	 * 应收应付类型
	 * 1 应收单; 2 其他应收单;3 收款单; 4 应收款转销; 5 应收款冲应付款; 
	 * -1 应付单; -2 其他应付单 ; -3 付款单; -4 应付款转销; -5 应付款冲应收款
	 * @return
	 */
	public Integer getTypeId() {
		return typeId;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	/**
	 * 单据编码
	 * @return
	 */
	public String getBillCode() {
		return billCode;
	}

	public void setBillCode(String billCode) {
		this.billCode = billCode;
	}

	/**
	 * 单据日期
	 * @return
	 */
	public Date getBillDate() {
		return billDate;
	}

	public void setBillDate(Date billDate) {
		this.billDate = billDate;
	}

	/**
	 * 客户ID
	 * @return
	 */
	public Integer getCustSuppId() {
		return custSuppId;
	}

	public void setCustSuppId(Integer custSuppId) {
		this.custSuppId = custSuppId;
	}

	/**
	 * 部门
	 * @return
	 */
	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	/**
	 * 职员
	 * @return
	 */
	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	/**
	 * 金额
	 * @return
	 */
	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	/**
	 * 剩余金额
	 * @return
	 */
	public BigDecimal getRemAmount() {
		return remAmount;
	}

	public void setRemAmount(BigDecimal remAmount) {
		this.remAmount = remAmount;
	}

	/**
	 * 核销状态
	 * @return
	 */
	public Integer getCheckStatus() {
		return checkStatus;
	}

	public void setCheckStatus(Integer checkStatus) {
		this.checkStatus = checkStatus;
	}

	/**
	 * 年份
	 * @return
	 */
	public Integer getFyear() {
		return fyear;
	}

	public void setFyear(Integer fyear) {
		this.fyear = fyear;
	}

	/**
	 * 期间
	 * @return
	 */
	public Integer getPeriod() {
		return period;
	}

	public void setPeriod(Integer period) {
		this.period = period;
	}

	/**
	 * 备注
	 * @return
	 */
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}
	
	/**
	 * 获取应收应付金额
	 * @return
	 */
	public BigDecimal getArpAmount() {
		if(direct == 1) {
			if(typeId == 1 || typeId == 2) {
				return amount;
			}
		} else {
			if(typeId == -1 || typeId == -2) {
				return amount;
			}
		}
		return null;
	}
	
	/**
	 * 获取客户往来的应收金额(包含核销)
	 * @return
	 */
	@XlsFieldAnnotation(ChsName="应收金额",MethodName="getARecAmount")
	BigDecimal arecAmount=BigDecimal.ZERO;
	public BigDecimal getARecAmount() {
		if(direct == 1) {
			if(typeId == 1 || typeId == 2) {
				if(isIncludeCheckTag()) {
					arecAmount= amount;
				} else {
					arecAmount= remAmount;
				}
			}
		}
		return arecAmount;
	}
	
	/**
	 * 获取客户往来的收款金额(包含核销)
	 * @return
	 */
	@XlsFieldAnnotation(ChsName="已收金额",MethodName="getRecAmount")
	BigDecimal recAmount=BigDecimal.ZERO;
	public BigDecimal getRecAmount() {
		if(direct == 1) {
			if(typeId >=3 && typeId <= 7) {
				if(isIncludeCheckTag()) {
					recAmount= amount;
				} else {
					recAmount= remAmount;
				}
			}
		}
		return recAmount;
	}
	
//	/**
//	 * 获取客户往来的应收金额(不包含核销)
//	 * @return
//	 */
//	public BigDecimal getARecAmountCheck() {
//		if(direct == 1) {
//			if(typeId == 1 || typeId == 2) {
//				return remAmount;
//			}
//		}
//		return null;
//	}
	
	/**
	 * 获取客户往来的应付金额(包含核销)
	 * @return
	 */
	@XlsFieldAnnotation(ChsName="应付金额",MethodName="getAPayAmount")
	BigDecimal apayAmount=BigDecimal.ZERO;
	public BigDecimal getAPayAmount() {
		if(direct == -1) {
			if(typeId == -1 || typeId == -2) {
				if(isIncludeCheckTag()) {
					apayAmount= amount;
				} else {
					apayAmount= remAmount;
				}
			}
		}
		return apayAmount;
	}
//	/**
//	 * 获取客户往来的应付金额(不包含核销)
//	 * @return
//	 */
//	public BigDecimal getAPayAmountCheck() {
//		if(direct == -1) {
//			if(typeId == -1 || typeId == -2) {
//				return remAmount;
//			}
//		}
//		return null;
//	}
	
	/**
	 * 获取已收已付金额
	 * @return
	 */
	
	public BigDecimal getRpAmount() {
		if(direct == 1) {
			if(typeId != 1 && typeId != 2 && typeId != 0) {
				return amount;
			}
		} else {
			if(typeId != -1 && typeId != -2 && typeId != 0) {
				return amount;
			}
		}
		return null;
	}
	
	
	
//	/**
//	 * 获取客户往来的收款金额(不包含核销)
//	 * @return
//	 */
//	public BigDecimal getRecAmountCheck() {
//		if(direct == 1) {
//			if(typeId >=3 && typeId <= 7) {
//				return remAmount;
//			}
//		}
//		return null;
//	}
	
	/**
	 * 获取客户往来的付款金额(包含核销)
	 * @return
	 */
	@XlsFieldAnnotation(ChsName="已付金额",MethodName="getPayAmount")
	BigDecimal payAmount=BigDecimal.ZERO;
	public BigDecimal getPayAmount() {
		if(direct == -1) {
			if(typeId <= -3 && typeId >= -7) {
				if(isIncludeCheckTag()) {
					payAmount= amount;
				} else {
					payAmount= remAmount;
				}
			}
		}
		return payAmount;
	}
	
//	/**
//	 * 获取客户往来的付款金额(不包含核销)
//	 * @return
//	 */
//	public BigDecimal getPayAmountCheck() {
//		if(direct == -1) {
//			if(typeId <= -3 && typeId >= -7) {
//				return remAmount;
//			}
//		}
//		return null;
//	}
	
	public String getTypeName() {
		   switch(typeId) {
		   case 1: 
			   billType = "应收单";
			   break;
		   case 2:
			   billType = "其他应收单";
			   break;
		   case 3:
			   billType = "收款单";
			   break;
		   case 4:
			   billType = "预收单";
			   break;
		   case 5:
			   billType = "退款单";
			   break;
		   case 6:
			   billType = "应收款转销";
			   break;
		   case 7:
			   billType = "应收款冲应付款";
			   break;
		   case -1:
			   billType = "应付单";
			   break;
		   case -2:
			   billType = "其他应付单";
			   break;
		   case -3:
			   billType = "付款单";
			   break;
		   case -4:
			   billType = "预付单";
			   break;
		   case -5:
			   billType = "退款单";
			   break;
		   case -6:
			   billType = "应付款转销";
			   break;
		   case -7:
			   billType = "应付款冲应收款";
			   break;
		   }
		   return billType;
	}

	/**
	 * 余额
	 * @return the endAmount
	 */
	//余额
	@XlsFieldAnnotation(ChsName="余额",MethodName="getEndAmount")
	private BigDecimal endAmount = BigDecimal.ZERO;
	public BigDecimal getEndAmount() {
		if(typeId == 0) {
			endAmount = amount;
		}
		return endAmount;
	}

	/**
	 * @param endAmount the endAmount to set
	 */
	public void setEndAmount(BigDecimal endAmount) {
		this.endAmount = endAmount;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#toString()
	 */
	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
		sb.append("id",this.id)
		  .append("typeId",this.typeId)
		  .append("amount",this.amount);
		
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}
		if(!(o instanceof RpContact)) {
			return false;
		}
		final RpContact contact = (RpContact)o;
		if(id != null) {
			return id.equals(contact.getId());
		} else {
			return contact.getId() == null;
		}
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#hashCode()
	 */
	@Override
	public int hashCode() {
		return (this.id != null ? this.id.hashCode() : 0);
	}

}
